//
//  CZHomeViewController.swift
//  微博08
//
//  Created by 1 on 16/2/28.
//  Copyright © 2016年 cjf. All rights reserved.
//

import UIKit

class CZHomeViewController: CZBaseTableViewController {
    
    // MARK: - 属性
    private let reuseIdentifier = "cellId"
    
    var statuses: [CZStatus]? {
        didSet{
            // 当别人给statuses属性赋值后，再刷新tableView
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // 只有用户登录了才需要做下面的操作
        if !CZUserAccountViewModel.sharedInstance.userLogin {
            return
        }
        setupNavigationBar()
        
        loadStatus()
        
        tableView.registerClass(CZStatusCell.self, forCellReuseIdentifier: reuseIdentifier)
    }
    private func loadStatus() {
        
        statusVM.loadStatus { (statuses, error) -> () in
            if error != nil {
                print("加载微博数据出错: \(error)")
                return
            }
            print("在控制器中打印微博数据: \(statuses)")
            self.statuses = statuses
            
        }
    }
    
    // 设置导航栏
    private func setupNavigationBar() {
        setupBarButtonItem()
        setupTitleView()
    }
    
    // 设置导航栏左右的item
    private func setupBarButtonItem() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop")
    }
    
    
    
    private func setupTitleView() {
        // 创建按钮
        let titleButton = CZTitleView(title: "我是标题")
        
        // 添加点击事件
        titleButton.addTarget(self, action: "titleButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        navigationItem.titleView = titleButton
    }
    
    // MARK: - 按钮点击事件
    func titleButtonClick(button: UIButton) {
        // 根据按钮的selected来确定按钮旋转的角度
        // selected = false, 箭头向下
        // selected = true, 箭头向上
        button.selected = !button.selected
        
        UIView.animateWithDuration(0.25) { () -> Void in
            button.imageView?.transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
        }
        
        // 按钮选中时弹出Popover
        if button.selected {
            // iOS8.0以后，使用UIPopoverPresentationController 可以达到Popover的效果
            // 1.加载sd
            let sb = UIStoryboard(name: "Popover", bundle: nil)
            
            // 2.创建控制器
            let popoverVC = sb.instantiateViewControllerWithIdentifier("popover")
            
            // 在iphone上面设置Popover会默认适配成Modal
            popoverVC.modalPresentationStyle = UIModalPresentationStyle.Popover
            // 设置popoverPresentationController代理
            popoverVC.popoverPresentationController?.delegate = self
            
            popoverVC.popoverPresentationController?.sourceView = navigationItem.titleView
            // 视配,箭头位置
            popoverVC.popoverPresentationController?.sourceRect = navigationItem.titleView!.bounds
            
            // 设置弹出控制器显示的大小
            popoverVC.preferredContentSize = CGSize(width: 200, height: 5 * 44)
            
            presentViewController(popoverVC, animated: true, completion: nil)
        }
    }
    
    // MARK: - 懒加载
    // 微博模型的ViewModel
    private lazy var statusVM: CZStatusViewModel = CZStatusViewModel()
    
}

// MARK: - tableView相关
extension CZHomeViewController {
    // 显示多少个cell
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // ??  ??前面的可选有值就拆包,如果没值就使用?? 后面的值
        return statuses?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath)
        // 将微博内容显示出来
//        cell.textLabel?.text = statuses?[indexPath.row].text
        return cell
    }
}

extension CZHomeViewController: UIPopoverPresentationControllerDelegate {
    // 让iphone上面可以显示popover效果
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }
    
    // popover关闭时调用
    func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
        // 将标题按钮的箭头转下去
        let button = navigationItem.titleView as! CZTitleView
        titleButtonClick(button)
    }
}
